package com.ningxia.water.service.impl;

import com.ningxia.water.entity.TsDayData;
import com.ningxia.water.entity.TsMonthData;
import com.ningxia.water.entity.TsWellMeter;
import com.ningxia.water.mapper.TsDayDataMapper;
import com.ningxia.water.mapper.TsMonthDataMapper;
import com.ningxia.water.mapper.TsWellMeterMapper;
import com.ningxia.water.service.ITsDayDataService;
import com.ningxia.water.service.ITsMonthDataService;
import com.ningxia.water.service.ITsWellMeterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningxia.water.vo.in.VolumeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hz
 * @since 2021-03-15
 */
@Service
public class TsWellMeterServiceImpl extends ServiceImpl<TsWellMeterMapper, TsWellMeter> implements ITsWellMeterService {

    @Autowired
    private TsWellMeterMapper wellMeterMapper;

    @Autowired
    private TsDayDataMapper dayDataMapper;

    @Autowired
    private TsMonthDataMapper monthDataMapper;

    @Override
    public Integer findWellMeters(Integer wellId) {
        return wellMeterMapper.findWellMeters();
    }

    /**
     * 天表聚合
     * 每天1点聚合前一天的数据
     *
     * @return
     */
    @Override
    public void groupDayVolume() throws Exception {
        //获取前一天时间
        String time = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //一天的00:00:00 和23:59:59
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()), ZoneId.systemDefault()).minusDays(1);
        //00:00:00
        String startTime = localDateTime.with(LocalDateTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //23:59:59
        String endTime = localDateTime.with(LocalDateTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        List<VolumeVo> list = wellMeterMapper.groupDayVolume(startTime, endTime);
        ListIterator<VolumeVo> it = list.listIterator();
        while (it.hasNext()) {
            VolumeVo vo = it.next();
            try {
                dayDataMapper.insert(new TsDayData(vo.getWellId(), vo.getVolume(), time));
            } catch (Exception e) {
                throw new Exception("执行错误");
            }
        }
    }

    /**
     * 月表聚合
     * 每天2点聚合前一天的数据
     *
     * @return
     */
    @Override
    public void groupMonthVolume() throws Exception {
        LocalDateTime now = LocalDateTime.now().minusDays(1);
        //获取天
        String dayTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //获取月
        String monthTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM"));

        List<VolumeVo> list = wellMeterMapper.groupMonthVolume(dayTime);
        ListIterator<VolumeVo> it = list.listIterator();
        while (it.hasNext()) {
            VolumeVo vo = it.next();
            try {
                monthDataMapper.insert(new TsMonthData(vo.getWellId(), vo.getVolume(), monthTime));
            } catch (Exception e) {
                throw new Exception("执行错误");
            }
        }
    }

}
